home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / glimpsehttp / wwwlib / contrib / aglimpse.wais < prev    next >
Encoding:
Text File  |  1995-05-16  |  8.0 KB  |  330 lines

  1. #!/usr/sbin/perl
  2. #
  3. # Acknowledgements
  4. #
  5. # Thanks to Guy Brooker (guy@jw.estec.esa.nl) for his AA interface,
  6. # which was the starting point for this program.
  7. #
  8. # Paul Clark
  9. # paul@cs.arizona.edu
  10. #
  11. # Modifications
  12. #
  13. # 2/22/94    Version 1.0, shell script version    Paul Clark
  14. # 4/21/94    Version 1.1, multiple archives support    Paul Clark
  15. # 4/22/94    Version 1.2, perl script        Paul Clark
  16. # 8/04/94    Version 1.2b, WAIS clone        Brian Behlendorf
  17. # 8/05/94        1.2b1, bug fixes        Brian Behlendorf
  18.  
  19. # **** **** **** ****    CONFIGURABLE VARIABLES     **** **** **** ****
  20. $HTTPD_HOME="/wired/www.tools/glimpse/" ;
  21. $GLIMPSE_LOC="/wired/www.tools/glimpse/bin/glimpse" ;
  22. $indexdir="/wired/www.tools/glimpse/indices";
  23. $serverroot="/wired/www/";
  24. $titledb="/wired/www.tools/db/files/titles";
  25.  
  26. # **** **** **** **** NO CONFIGURATION NEEDED BELOW **** **** **** ****
  27.  
  28. # open the title DBM file
  29.  
  30. dbmopen (%TTLDB, "$titledb", 0664);
  31.  
  32. $path = $ENV{'PATH_INFO'};
  33.  
  34. #if ( m|^/([0-9][0-9]*)(.*)$| ) {
  35. #    $script = $1;
  36. #    $path = $2;
  37. #}# else {
  38. #    &err_nopathinfo;
  39. #}
  40.  
  41. ($ENV{'HOME'} = $indexdir) || &err_cantcwd; 
  42.  
  43. # Ensure that Glimpse is available on this machine
  44. -x $GLIMPSE_LOC || &err_noglimpse ;
  45.  
  46. # Ensure that index is available
  47. -r "$indexdir/.glimpse_index" || &err_noindex ;
  48.  
  49. #    To support an ISINDEX type search, set query string if given
  50. #    an argument on the command line
  51. $prefix="whole=on&case=off&query=" if ( $#ARGV >= 0 );
  52.  
  53. #    Check that a query has been made
  54. ($query = $ENV{'QUERY_STRING'}) || &err_newquery ;
  55.  
  56. #    Strip the variables out from the query string,
  57. #    and assign them into variables, prefixed by 'QS_'
  58. $query =~ s/%0D//g;
  59. $query =~ s/%20/+/g;
  60. @qvars = split( /\&/, $prefix . $query );
  61. foreach (@qvars) {
  62.     split(/=/);
  63.     $fname = $_[0];
  64.     $fvalue = $_[1];
  65.     $cmd = "\$QS_$fname = \"$fvalue\";" ;
  66.     $cmd = eval $cmd if ( $fname =~ /^[a-z_A-Z]\w*$/ );
  67. }
  68. #chop $QS_query;
  69. $OPT_errors="-$QS_errors"    if $QS_errors =~ /^[0-8]$/;
  70. $OPT_errors="-B -y"        if $QS_errors =~ /^Best\+match$/;
  71. $OPT_case="-i"            unless $QS_case =~ /^on$/;
  72. $OPT_whole="-w"            unless $QS_whole =~ /^on$/;
  73. $OPT_filter="-F \"$path\""    if $path;
  74.  
  75. print "Content-type: text/html\n\n" ;
  76. print "<HEAD><TITLE>Glimpse response for query \"$QS_query\"\n";
  77. print "</TITLE></HEAD><BODY>\n";
  78. ($html_query = $QS_query) =~ s/\+/ /g;
  79. print "<H1>Glimpse search for \"$html_query\"</H1><HR>\n";
  80. @args = split(/\+/, $QS_query);
  81. while (@args) {
  82.     $tmp = shift(@args);
  83.     $tmp =~    s/^OR/,/g;
  84.     $tmp =~ s/^AND/;/g;
  85.     $total_query = "$total_query$tmp";
  86. }
  87. #print "You're looking for: $total_query";
  88.  
  89. if (index($total_query, "\|") > 0) {
  90.     if ($OPT_whole eq "-w" ) {
  91.         $OPT_whole = "";
  92.         print "<P>Substring searches not supported with the OR operation, for some reason.</B>";
  93.     }
  94. }
  95.  
  96. chdir $indexdir;
  97. $cmd = "$GLIMPSE_LOC -y -c $OPT_case $OPT_whole $OPT_errors -H . " .
  98.      "$OPT_filter \'$total_query\' 2>&1 |";
  99. ($html_cmd = $cmd) =~ s/>/>/g;
  100. print "<P><!-- Command = \"$html_cmd\" -->";
  101. open(GOUT, $cmd );
  102. while (<GOUT>) {
  103.     s/&/\&/g;
  104.     s/</\</g;
  105.     s/>/\>/g;
  106.     ( /^([^ ]*):\s*([0-9][0-9]*)/) || next;
  107.     $file = $1;
  108.     $count = $2;
  109.     ($a, $b, $c, $d, $e, $f, $g, $length, @i) = stat($file);
  110.     $file = substr($file, length($serverroot));
  111.     $file =~ s/^\/*/\//g ;
  112.     $rel = $count*1000/$length;
  113.     $files{$rel}=$file;
  114. }
  115.  
  116. ($scale, @rest) = (reverse sort(keys %files));
  117.  
  118. $scale = 1000/$scale if ($scale);
  119.  
  120. print "<dl>\n";
  121. $count = 0;
  122. foreach $key (reverse sort(keys %files)) {
  123.     $count++;
  124.     $cleankey = int($key*$scale);
  125.     $title = &get_title($files{$key});
  126.     print "<dt><B>", $count, ":</B> <a href=\"", $files{$key}, "\">", $title, "</a>\n";
  127.     print "<dd>Score: <B>", $cleankey, "</B>\n";
  128. }
  129.  
  130. print "</DL>\n";
  131. print "<B>No items found.  Either that word doesn't appear anywhere or it's so common (like \"an\" or \"the\") it doesn't get indexed.</B><P>" if ($count < 1);
  132.  
  133. print "<HR>" ;
  134. print "Many thanks to <a href=\"http://glimpse.cs.arizona.edu:1994/\">Glimpse</a>\n";
  135. print "</BODY>\n" ;
  136. close(GOUT);
  137.  
  138. sub diag_exit {
  139. # exit on error
  140.     exit 1;
  141. }
  142.  
  143. sub err_newquery {
  144. #    The script was called without a query. 
  145. #    Provide an ISINDEX type response for browsers
  146. #    without form support.
  147.     print <<'EOM' ;
  148. Content-type: text/html
  149.  
  150. <HEAD>
  151. <TITLE>Glimpse Search of Wired's Web Site</TITLE>
  152. </HEAD>
  153. <BODY>
  154. EOM
  155.     $path = "/" unless $path;
  156.     ($foo = "$path/index.html") =~ s/\/\//\//g;
  157.     ($namepath = &get_title($foo)) =~ s/index.html//g;    
  158.     print "<H2>Search Within \"$namepath\"</H2>\n";
  159.     print "<FORM ACTION=\"/cgi-bin/aglimpse$path\">\n";
  160.     print "If your browser does not support forms, <a href=\"/cgi-bin/aglimpsenf$path\">use the form-free version.</a>\n";
  161.  
  162.     print <<'EOM' ;
  163. <P>You can enter one search term or a 
  164. logical combination of two or more terms.<BR>
  165. For example: <B>"Wired AND Digital"</B> or 
  166. <B>"Barney OR TV"</B>.
  167. <P>
  168. <INPUT NAME="query" size=80> 
  169. <P>
  170. <INPUT NAME="case" TYPE="checkbox">Make this search case sensitive<BR>
  171. <INPUT NAME="whole" TYPE="checkbox">Look for this word (these words) within other words
  172. <P>
  173.  
  174. You can also select specify a range of error in your search terms. For
  175. example, with an error range set to 1, a search for "turkey" can also
  176. return "turnkey". One can also search for a "best match", meaning
  177. it'll look for the word with 0 errors, then 1 error, then 2 errors,
  178. etc.  Usually ranges of 1 or 2 are useful for common spelling errors.
  179.  
  180. <P>
  181. <SELECT NAME="errors">
  182. <OPTION>0
  183. <OPTION>1
  184. <OPTION>2
  185. <OPTION>3
  186. <OPTION>4
  187. <OPTION>5
  188. <OPTION>6
  189. <OPTION>7
  190. <OPTION>8
  191. <OPTION>Best match
  192. </SELECT>
  193. <P>
  194. <INPUT TYPE="submit" VALUE="Begin Search">
  195. <INPUT TYPE="reset" VALUE="Reset Values">
  196. EOM
  197.     if ($path) {
  198.     print "<P>You are currently searching below the \"$namepath\" directory.\n";
  199.     $path =~ (s/;/\//g);
  200.     $cmd = "ls -l $serverroot$path | grep \"^d\" |";
  201.     open (LIST, $cmd);
  202.     $titlcount = 0;
  203.     while (<LIST>) {
  204.         print "You can restrict your search by clicking on one of these subdirectories:\n\n<UL>\n" unless ($titlcount++);
  205.         @sub = split(/\s+/);
  206.         $tmp2 = $sub[8];
  207.         ($tmp3 = "$serverroot$path$tmp2/index.html") =~ s/\/+/\//g;
  208.         if (-e "$tmp3") {
  209.             $dirname = &get_title("$path$tmp2/index.html")
  210.         } 
  211.         $dirname = "$path$tmp2" unless ($dirname);
  212.         $dirname =~ s/\/index.html//g;
  213.         print "<LI><a href=\"/cgi-bin/aglimpse$path$tmp2/\">$dirname</a>\n";
  214.         $dirname = "";
  215.     }
  216.     close (LIST);
  217.     print "</UL>\n";
  218.     }    
  219.     print "</FORM></BODY>\n";
  220.     print "Many thanks to <a href=\"http://glimpse.cs.arizona.edu:1994/\">Glimpse</a>\n";
  221.  
  222.     &diag_exit;
  223.  
  224. }
  225.  
  226. sub err_noglimpse {
  227. #
  228. # Glimpse was not found
  229. # Report a useful message
  230. #
  231.     print <<'EOM' ;
  232. Content-type: text/html
  233.  
  234. <HEAD>
  235. <TITLE>Glimpse not found</TITLE>
  236. </HEAD>
  237. <BODY>
  238. <H1>Glimpse not found</H1>
  239.  
  240. This gateway relies on <CODE>Glimpse</CODE> search tool.
  241. If it is installed, please set the correct path in the script file.
  242. Otherwise obtain the latest version from
  243. <A HREF="file://ftp.cs.arizona.edu/glimpse">ftp.cs.arizona.edu</A>
  244. </BODY>
  245. EOM
  246.     &diag_exit;
  247. }
  248.  
  249. sub err_noindex {
  250. # Glimpse index was not found
  251. # Give recommendations for indexing
  252.     print <<'EOM' ;
  253. Content-type: text/html
  254.  
  255. <HEAD>
  256. <TITLE>Glimpse Index not found</TITLE>
  257. </HEAD>
  258. <BODY>
  259. <H1>Glimpse Index not found</H1>
  260. Glimpse cannot proceed without index.
  261. Please check if the directory being searched is indexed
  262. by <code>glimpseindex</code>.
  263. </BODY>
  264. EOM
  265.     &diag_exit;
  266. }
  267.  
  268. sub print_it {
  269.     print <<'EOM' ;
  270. Content-type: text/html
  271.  
  272. <HEAD>
  273. <TITLE>Glimpse Index not found</TITLE>
  274. </HEAD>
  275. <BODY>
  276. <H1>Glimpse Index not found</H1>
  277. You were looking for [$1].
  278. by <code>glimpseindex</code>.
  279. </BODY>
  280. EOM
  281. #    &diag_exit;
  282. }
  283.  
  284. sub err_noquery {
  285. # No path_info was there.
  286.     print <<'EOM' ;
  287. Content-type: text/html
  288.  
  289. <HEAD>
  290. <TITLE>No search terms submitted</TITLE>
  291. </HEAD>
  292. <BODY>
  293. <H1>No search terms submitted</H1>
  294. Please enter a term to search for.
  295. <ISINDEX ACTION="/cgi-bin/aglimpse">
  296. </BODY>
  297. EOM
  298.     &diag_exit;
  299. }
  300.  
  301. sub err_cantcwd {
  302. # No path_info was there.
  303.     print <<'EOM' ;
  304. Content-type: text/html
  305.  
  306. <HEAD>
  307. <TITLE>Error: index path incorrect</TITLE>
  308. </HEAD>
  309. <BODY>
  310. <H1>Index Path Incorrect</TITLE>
  311. Configuration error on the server side.
  312. <ISINDEX ACTION="/cgi-bin/aglimpse">
  313. </BODY>
  314. EOM
  315.     &diag_exit;
  316. }
  317.  
  318. sub get_title {
  319. # get the title of an HTML doc
  320.     $gfile = shift(@_);
  321.     ($otherfile = "$serverroot/$gfile") =~ s/\/+/\//g;
  322.     $G = $TTLDB{$otherfile};
  323.     if ($G) {
  324.         return $G;
  325.     } else {
  326.         return $gfile;
  327.     }
  328. }
  329.  
  330.